#include <types.h>
#include <memory.h>
#include <stdio.h>


extern uint32_t bootloader_memsize;
extern uint32_t bss_end[]; // End of the kernel in memory after bootloader.

uint32_t *kern_end; // End of the kernel in memory after ph_alloc_init.
static page_t *ph_page_map;
static uint32_t *ph_page_stacktop;


void
ph_alloc_init()
{
	uint32_t ph_num_pages, idx;
	page_t *ph_page;
	
	// Number of available physical pages available.
	ph_num_pages = bootloader_memsize/4;	// Each page is 4kB.
	
	// Fill the map of available physical pages. Start at the
	// end of the kernel as loaded by the bootloader.
	ph_page_map = (page_t *)&bss_end[0];
	for( idx = 0, ph_page = ph_page_map;
		idx < ph_num_pages;
		idx++, ph_page++ )
	{
		ph_page->addr = (uint32_t *)(idx*PG_SIZE);
		ph_page->count = 0;
	}
	
	// End of the physical page map is the new end of the kernel in memory.
	kern_end = (uint32_t *)ph_page;
	
	// Fill the stack of addresses of free physical pages. End of the
	// physical page stack will be the new end of the kernel in memory.
	for( idx = ph_num_pages; idx > 0; idx--, kern_end++)
	{
		*kern_end = (idx - 1)*PG_SIZE;
	}
	
	ph_page_stacktop = kern_end;
}

uint32_t
ph_alloc()
{
	return 0;
}

void
ph_dealloc(uint32_t ph_page)
{
	
}